package com.xishu.service.print.kitchen;

import com.xishu.entity.hardware.Printer;
import com.xishu.entity.order.Order;
import com.xishu.entity.order.OrderItem;
import com.xishu.util.ClassUtil;
import com.xishu.util.Tools;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.apache.commons.chain.impl.ChainBase;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.stream.Collectors;

public class PrinterOrderCmd implements Command, Tools {
    private static Logger logger = LoggerFactory.getLogger(PrinterOrderCmd.class);
    private Printer printer;
    private Order orderInDb;
    private List<Long> orderItemIdList;

    public PrinterOrderCmd(Printer printer, Order orderInDb, List<Long> orderItemIdList) {
        this.printer = printer;
        this.orderInDb = orderInDb;
        this.orderItemIdList = orderItemIdList;
    }

    /**
     * 依次执行，不结束
     *
     * @param context
     * @return
     * @throws Exception
     */
    @Override
    public boolean execute(Context context) throws Exception {
        //复制一份数据到打印机订单，以避免修改了以前的数据
        Order printOrder = (Order) ClassUtil.clone(orderInDb);

        //过滤出这次需要打印的菜品清单
        List<OrderItem> printerOrderItemList = getList(printOrder.getOrderItemDetailList()).stream().filter(p -> {
            return orderItemIdList.contains(p.getId().longValue());
        }).collect(Collectors.toList());

        //过滤出哪些是要该打印机的菜品
        List<String> printerLabelList = printer.getPrinterLabelList();

        logger.info("printerLabelList is {}", StringUtils.join(printerLabelList, ","));

        if (isEmpty(printerLabelList)) {
            logger.info("printer label list is empty");
            return false;
        }

        //过虑该打印机需要打印的菜
        printerOrderItemList = printerOrderItemList.stream().filter(orderItem -> {
            return KitchenService.getInstance().isItemContainThePrintLabel(printerLabelList, orderItem);
        }).collect(Collectors.toList());

        //将套餐里面的菜拆出来，然后同一个标签的，一起打印
        for (OrderItem orderItem : printerOrderItemList) {
            KitchenService.getInstance().filterValidLabelInSetMenuItem(printerLabelList, orderItem);
        }

        //没有菜，直接返回
        if (isEmpty(printerOrderItemList)) {
            logger.info("{} has no order item detail list", printer.getId());
            return false;
        }

        printOrder.setOrderItemDetailList(printerOrderItemList);

        logger.info("printer order is {}", printOrder);
        //一菜一单打印
        if (getBoolean(printer.getOneItemOneOrder())) {
            ChainBase chainBase = new ChainBase();
            chainBase.addCommand(new CombineOrderCmd(printOrder));
            chainBase.addCommand(new SplitOrderItemCmd(printOrder));

            chainBase.execute(context);
        }else {
            //整个菜一单打印
            ChainBase chainBase = new ChainBase();
            chainBase.addCommand(new CombineOrderCmd(printOrder));
            chainBase.execute(context);
        }


//        sendOrderToKitchen(printOrder, printer.id);

        return false;
    }
}
